package com.ktjy.springthymeleaf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ktjy.springthymeleaf.entity.User;
import com.ktjy.springthymeleaf.mapper.UserMapper;
import com.ktjy.springthymeleaf.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @return 13141
 * 2025/7/15
 * 19:49
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public int saveOrUpdate(User user, String usrName) {

        if (user.getUsrId() == null) {
            return userMapper.insert(user);
        } else {
            QueryWrapper<User> wrapper = new QueryWrapper<User>();
            wrapper.eq("usr_name", usrName);
            return userMapper.update(user, wrapper);
        }
    }

    @Override
    public List<User> list() {
        return userMapper.selectList(null);
    }

    @Override
    public User login(String usrName, String usrPassword) {
        User user = userMapper.getUserByUsrNameWithRoleName(usrName);
        // 判断用户是否存在
        if (user != null) {
            // 判断密码是否正确
            if (user.getUsrPassword().equals(usrPassword)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteUser(Long usrId) {
        return userMapper.deleteById(usrId);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUser(Long usrId) {
        return userMapper.selectById(usrId);
    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.getUserListWithRoleName();
    }
}
